Android MediaPlayer 问题:“错误 (-38 , 0)”和“在状态 1 中停止调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11913108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:09:45  来源:igfitidea点击:

Android MediaPlayer Problems :"Error (-38 , 0) " and "stop called in state 1"

androidmedia-playerandroid-mediaplayer

提问by jy2014

It's no sound from my device even the emulator.

即使是模拟器,我的设备也没有声音。

Actually, at the line "mMediaPlayer.create(this, musicIds[0]); ", eclipse suggests me two choices : "Change access to static using 'MediaPlayer'(declaring type)"or "Add @SuppressWarnings 'static-access' to on Create()". By the way, I take eclipse's suggestions........but still don't work.

实际上,在“mMediaPlayer.create(this, musicIds[0]);”这一行,eclipse 建议我两个选择:“使用'MediaPlayer'(声明类型)更改对静态的访问“添加@SuppressWarnings 'static-access'在 Create()" 上。顺便说一句,我接受了 eclipse 的建议……但仍然不起作用。

P.S. I called a package from other project (pager).

PS我从其他项目(寻呼机)调用了一个包。

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setTitle(R.string.diff_title);
    setContentView(R.layout.title_layout);    


    viewFlow = (ViewFlow) findViewById(R.id.viewflow);
    DiffAdapter adapter = new DiffAdapter(this);
    viewFlow.setAdapter(adapter);  
    TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
    indicator.setTitleProvider(adapter);
    viewFlow.setFlowIndicator(indicator);


    //Set all views
    listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listeStrings));
    final ImageView imv=(ImageView)findViewById(R.id.imv);
    imv.setImageResource(imageIds[0]);
    final ImageView pic = (ImageView)findViewById(R.id.Picture);
    pic.setImageResource(imageIds[0]);

    btnPlay = (ImageButton)findViewById(R.id.btnplay);
    btnPlay.setOnClickListener(this);

    btnNext = (ImageView)findViewById(R.id.imvnext);
    btnLast = (ImageView)findViewById(R.id.imvlast);        
    final TextView textview = (TextView)findViewById(R.id.textView2);
    textview.setText(listeStrings[0]);  
    seekBar = (SeekBar)findViewById(R.id.seekbar);
    //seekBar.setMax(mMediaPlayer.getDuration());

    mMediaPlayer.create(this, musicIds[0]);

    try {
           if (mMediaPlayer != null) {
               mMediaPlayer.stop();
           }
           mMediaPlayer.prepare();
           mMediaPlayer.start(); 
    } catch (IllegalStateException e) {
           e.printStackTrace();
    } catch (IOException e) {
           e.printStackTrace();
    }

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int songIndex = position;
            String songTitle=musicNames[position];
            imv.setImageResource(imageIds[position]);
            pic.setImageResource(imageIds[position]);
            textview.setText(listeStrings[position]);

        }
    });


}


}

And this is my logcat:

这是我的 logcat:

08-11 07:33:19.217: E/MediaPlayer(338): stop called in state 1
08-11 07:33:19.217: E/MediaPlayer(338): error (-38, 0)
08-11 07:33:19.217: E/MediaPlayer(338): prepareAsync called in state 0
08-11 07:33:19.217: W/System.err(338): java.lang.IllegalStateException
08-11 07:33:19.227: W/System.err(338):  at android.media.MediaPlayer.prepare(Native Method)
08-11 07:33:19.227: W/System.err(338):  at org.taptwo.android.widget.viewflow.example.DiffViewFlowExample.onCreate(DiffViewFlowExample.java:95)
08-11 07:33:19.227: W/System.err(338):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-11 07:33:19.227: W/System.err(338):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-11 07:33:19.227: W/System.err(338):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-11 07:33:19.227: W/System.err(338):  at android.app.ActivityThread.access00(ActivityThread.java:117)
08-11 07:33:19.227: W/System.err(338):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-11 07:33:19.227: W/System.err(338):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 07:33:19.227: W/System.err(338):  at android.os.Looper.loop(Looper.java:123)
08-11 07:33:19.227: W/System.err(338):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-11 07:33:19.227: W/System.err(338):  at java.lang.reflect.Method.invokeNative(Native Method)
08-11 07:33:19.227: W/System.err(338):  at java.lang.reflect.Method.invoke(Method.java:507)
08-11 07:33:19.227: W/System.err(338):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-11 07:33:19.227: W/System.err(338):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-11 07:33:19.227: W/System.err(338):  at dalvik.system.NativeStart.main(Native Method)<br>
08-11 07:33:19.257: E/MediaPlayer(338): Error (-38,0)

回答by Amir Uval

Before prepare(), you need first to call setDataSource(..).

之前prepare(),你需要先打电话setDataSource(..)

The Media framework is a very strict state machine, and it's really cumbersome to handle all the different states.

Media 框架是一个非常严格的状态机,处理所有不同的状态真的很麻烦。

I've used this little wrapperthat makes the coding/debugging a bit easier. You can give it a try.

我使用了这个小包装器,它使编码/调试更容易一些。你可以试一试。

Regarding emulator - note that not all file formats are supported on it.

关于模拟器 - 请注意,并非所有文件格式都受其支持。