Java 媒体播放器错误 -38,0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18482018/
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-12 02:02:10  来源:igfitidea点击:

mediaPlayer error -38,0

javaandroidandroid-mediaplayer

提问by Coma White

I try to do simple online radio player. Here is adress of stream http://radio-electron.ru:8000/96Here is my code.

我尝试做简单的在线广播播放器。这是流的地址http://radio-electron.ru:8000/96这是我的代码。

MyActivity.java

我的活动.java

package com.example.untitled2;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MyActivity extends Activity {

    MediaPlayer mediaPlayer;
    Button playButton;


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

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mediaPlayer = new MediaPlayer();
        playButton = (Button)findViewById(R.id.button);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mediaPlayer.isPlaying())
                    mediaPlayer.stop();
                else {
                    try {

                        mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("http://radio-electron.ru:8000/96"));
                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                            @Override
                            public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
                                Toast.makeText(getApplicationContext(), "ERROR " + i, Toast.LENGTH_LONG).show();
                                playButton.setEnabled(false);
                                Log.d("radio", "error " + i + " " + i2);
                                return false;  //To change body of implemented methods use File | Settings | File Templates.
                            }
                        });
                        mediaPlayer.prepareAsync();
                    }
                    catch (IOException e) {
                        Toast.makeText(getApplicationContext(), "ERROR " + e, Toast.LENGTH_LONG).show();
                        Log.d("radio", "error " + e);
                    }
                    mediaPlayer.start();
                }
            }
        });
    }
}

main.xml

主文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
        >
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_gravity="center"/>
</FrameLayout>

I have no idea, why i get -(38, 0) code and what does it mean. I using Intellij IDEA and trying this code on Android 2.3 and 4.2 emulators and get some problem.

我不知道为什么我得到 -(38, 0) 代码以及它是什么意思。我使用 Intellij IDEA 并在 Android 2.3 和 4.2 模拟器上尝试此代码并遇到一些问题。

采纳答案by allprog

-38 refers to ENOSYSerror code from errno.h(see this explanation https://stackoverflow.com/a/15206308/768935)

-38 指的是errno.h 中的ENOSYS错误代码(见这个解释https://stackoverflow.com/a/15206308/768935

You seem to try to start the playing before the preparation is complete. Use the setOnPreparedListener()method to set a preparation listener and call the start()method only after the preparation is complete.

您似乎在准备完成之前尝试开始播放。使用该 setOnPreparedListener()方法设置准备监听器,并start()在准备完成后调用该方法。

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  public void onPrepared(MediaPlayer mp) {
      mp.start();
  }
});
mediaPlayer.prepareAsync();

And remove the current mediaPlayer.start()invocation from the code.

mediaPlayer.start()从代码中删除当前调用。

回答by Michael

The error code -38 ought to correspond to INVALID_OPERATION.

错误代码 -38 应该对应于INVALID_OPERATION.

A likely cause of this is that you don't wait for prepareAsyncto finish before you call start. You should set an onPreparedListenerand start the MediaPlayeronly when onPreparedhas been called.

造成这种情况的一个可能原因是您prepareAsync在调用 之前没有等待完成start。您应该设置一个onPreparedListenerMediaPlayeronPrepared被调用时启动。

回答by g-hos

@allprog and @Michael are right. But there is another way, if you don't want to use prepareAsync(), use prepare(). That is blocking, which is returned only when it is prepared.

@allprog 和@Michael 是对的。但是还有另一种方法,如果您不想使用prepareAsync(),请使用prepare()。那就是阻塞,只有在准备好时才返回。

回答by Unknownweirdo

I was getting my -38error by calling getDuration();on the MediaPlayer beforeit was prepared.

-38通过getDuration();在准备之前调用MediaPlayer 来得到我的错误。



It's worth checking the MediaPlayerdoc.

值得查看MediaPlayer文档。

There is a paragraph that starts It is a programming error to invoke methods such as getCurrentPosition()...

有一段开头 It is a programming error to invoke methods such as getCurrentPosition()...

that has a list of methods which are un-ideal to call before the MediaPlayer is prepared, which in turn may result in -38.

它有一个方法列表,在准备 MediaPlayer 之前调用这些方法是不理想的,这反过来可能会导致-38.

回答by pilotmario

You better check if you are executing any operation that is related to the playing statelike getCurrentPosition()before the Mediaplayer is started.

你最好检查一下,如果你正在执行其有关的任何操作播放状态类似getCurrentPosition()的媒体播放器开始之前。