0

我一直在尝试播放 MP3 文件。它只是行不通。

几天前,我确实设法使用相同/相似的代码向它添加了各种功能。只是试图重新做一遍,现在这些错误......

有人可以指出错误以及为什么会发生这种情况。

只是为了澄清我使用了不同格式的 URI,它们都不起作用。认为也许它的准备方法。

package com.player.phoneagent.gui;


import java.io.IOException;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;


public class AndroidClientActivity extends Activity {
    /** Called when the activity is first created. */

    private MediaPlayer mp;
    String path = "android.resource://com.player.phoneagent/raw/test";

    ImageButton btn_prev;
    ImageButton btn_play;
    ImageButton btn_pause;
    ImageButton btn_next;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn_prev = (ImageButton) findViewById(R.id.ImageButton01);
        btn_pause = (ImageButton) findViewById(R.id.ImageButton02);
        btn_play = (ImageButton) findViewById(R.id.ImageButton03);
        btn_next = (ImageButton) findViewById(R.id.ImageButton04);

        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);


        btn_play.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try {
                    mp.setDataSource(path); 

                } catch (IllegalArgumentException e) {

                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    mp.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mp.start();

            }
        });

    }

}






08-01 07:06:00.142: W/KeyCharacterMap(407): No keyboard for id 0
    08-01 07:06:00.142: W/KeyCharacterMap(407): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    08-01 07:06:05.821: D/dalvikvm(407): GC_EXPLICIT freed 21K, 53% free 2568K/5379K, external 3207K/3962K, paused 55ms
    08-01 07:06:20.311: D/dalvikvm(444): GC_EXTERNAL_ALLOC freed 52K, 53% free 2552K/5379K, external 1969K/2137K, paused 58ms
    08-01 07:06:43.171: E/MediaPlayer(444): error (1, -2147483648)
    08-01 07:06:43.171: W/System.err(444): java.io.IOException: Prepare failed.: status=0x1
    08-01 07:06:43.171: W/System.err(444):  at android.media.MediaPlayer.prepare(Native Method)
    08-01 07:06:43.171: W/System.err(444):  at com.player.phoneagent.gui.AndroidClientActivity$1.onClick(AndroidClientActivity.java:60)
    08-01 07:06:43.171: W/System.err(444):  at android.view.View.performClick(View.java:2485)
    08-01 07:06:43.171: W/System.err(444):  at android.view.View$PerformClick.run(View.java:9080)
    08-01 07:06:43.181: W/System.err(444):  at android.os.Handler.handleCallback(Handler.java:587)
    08-01 07:06:43.181: W/System.err(444):  at android.os.Handler.dispatchMessage(Handler.java:92)
    08-01 07:06:43.181: W/System.err(444):  at android.os.Looper.loop(Looper.java:123)
    08-01 07:06:43.181: W/System.err(444):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    08-01 07:06:43.181: W/System.err(444):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-01 07:06:43.181: W/System.err(444):  at java.lang.reflect.Method.invoke(Method.java:507)
    08-01 07:06:43.181: W/System.err(444):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    08-01 07:06:43.181: W/System.err(444):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    08-01 07:06:43.181: W/System.err(444):  at dalvik.system.NativeStart.main(Native Method)
    08-01 07:06:43.181: E/MediaPlayer(444): start called in state 0
    08-01 07:06:43.181: E/MediaPlayer(444): error (-38, 0)
    08-01 07:06:43.181: E/MediaPlayer(444): Error (-38,0)
4

2 回答 2

1

我总是在尝试使用“原始”文件夹时遇到问题......我建议将声音文件放在“资产”中并使用此处的方法来设置/准备/和播放:

http://droidapp.co.uk/2011/06/08/android-dev-playing-a-mp3-from-assets/

于 2012-08-01T06:23:40.573 回答
1

只需执行以下操作:

MediaPlayer mp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       

    setContentView(R.layout.main);       

    mp = MediaPlayer.create(this, R.raw.yourSongFileName);

    btn_play.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mp.stop();
            mp.release();
            mp = null;

            mp = MediaPlayer.create(this, R.raw.yourSongFileName);

            mp.start();
            //Other stuff below.

看看这是否有效。

于 2012-08-01T06:28:06.793 回答