Android mediaplayer错误代码(-38,0)

时间:2013-11-25 10:08:38

标签: android android-mediaplayer soundcloud

我正在尝试在我的Android应用上播放媒体文件。

public class MainActivity extends Activity implements Runnable {


    private  static final MediaPlayer mp = new MediaPlayer();
    //private ProgressBar progressBar;
    private TextView pauseicon;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pauseicon = (TextView) findViewById(R.id.textView_PlayPause);
        //progressBar = (ProgressBar) findViewById(R.id.progressBar);
        //getActionBar().setDisplayHomeAsUpEnabled(true);
        pauseicon.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String res = "https://api.soundcloud.com/tracks/84973999/stream?client_id=cd9d2e5604410d714e32642a4ec0eed4";

                //final MediaPlayer mp = new MediaPlayer();
                try {

                    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mp.setDataSource(res);
                    mp.prepare();


                    //mp.start();
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            // TODO Auto-generated method stub

                            mp.start();

                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }

                mp.setOnCompletionListener(new OnCompletionListener() {
                    //When audio is done will change pause to play
                    public void onCompletion(MediaPlayer mp) {
                        pauseicon.setText("Play file again");
                    }
                });

            }
        });


        /**
         * Play button click event
         * plays a song and changes button to pause image
         * pauses a song and changes button to play image
         * */




        pauseicon.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                // No need to check if it is pauseicon

                if(mp.isPlaying()){
                    mp.pause();
                    ((TextView) v).setText("play");

                } else {
                    mp.start();
                    ((TextView) v).setText("pause");
                }}});



    }

    /*

    //To update progress bar
    public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<=total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            progressBar.setProgress(currentPosition);
        }
    }*/
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);


            if (mp != null)
                if(mp.isPlaying())
                    mp.stop();

            mp.release();

            return true;
        default:
            return super.onOptionsItemSelected(item);

        }
    }
    @Override 
    public void onBackPressed(){
        if (mp != null){
            if(mp.isPlaying())
                mp.stop();

            mp.release();
        }

        //there is no reason to call super.finish(); here
        //call super.onBackPressed(); and it will finish that activity for you
        super.onBackPressed(); 

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }}

我正在11-25 15:36:48.319: E/MediaPlayer(11800): Error (-38,0)

Manisfest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demosc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.demosc.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.INTERNET" />

    <permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>

2 个答案:

答案 0 :(得分:2)

您正在设置onClickListener,您可以在其中设置MediaPlayer:

pauseicon.setOnClickListener(new OnClickListener() {
//code

但是,你这样做了:

pauseicon.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            // No need to check if it is pauseicon

            if(mp.isPlaying()){
                mp.pause();
                ((TextView) v).setText("play");

            } else {
                mp.start();
                ((TextView) v).setText("pause");
            }}});

将覆盖您之前设置的onClickListener。所以最后,当你点击pauseicon时,mp.pause()可能会给你错误,因为mp还没有被初始化。

修改 看一下MediaPlayer示例here

此外,在处理MediaPlayer时,请记住音频/视频文件和流的播放控制是作为状态机进行管理的,因此调用方法的顺序很重要。 MediaPlayer state diagram

更多信息here

另外,如ThaMe90所述,您可以考虑将onClick()之外的MediaPlayer初始化代码移动到onCreate()方法中,例如在调用onPrepare()时启用PLAY按钮。

答案 1 :(得分:0)

要从原始文件播放视频,请使用以下代码

1-将所有视频(.MP4)放入/ res / raw文件夹 在活动内部2写下这段代码。

   Uri video;
   VideoView videoHolder = new VideoView(this);
   setContentView(videoHolder);
   video = Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.xXXX);

  videoHolder.setVideoURI(video);
  videoHolder.setOnCompletionListener(new OnCompletionListener() {
  public void onCompletion(MediaPlayer mp) {
  jumpMain(); // jump to the next Activity
    }
});
  videoHolder.start();
   videoHolder.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ((VideoView) v).stopPlayback();
                jumpMain();
                return true;
}
});

       } catch (Exception ex) {
        jumpMain();
    }


    @Override
public boolean onTouchEvent(MotionEvent ev) {
    return false;
}

private synchronized void jumpMain() {
    Intent intent = new Intent(ShowVideoFragment.this, MainActivity.class);
    startActivity(intent);
    finish();

}