gpt4 book ai didi

android - 为媒体播放器导入原始 mp3 资源

转载 作者:太空狗 更新时间:2023-10-29 12:57:02 25 4
gpt4 key购买 nike

我正在尝试使用以下代码播放一个简单的 mp3 文件:

package swalehm.android.examples.myTest1;
import swalehm.android.examples.myTest1.R;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;

public class myTest1Main extends Activity
{
Context context;

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

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
mp.start();
}
}

我在“res”文件夹中添加了一个名为“raw”的文件夹,其中包含文件 sound1.mp3。

我检查了 R.java。命名资源 sound1 确实存在于该文件中。然而,当我构建它,我收到一条错误消息,指出 sound1 无法解析或不是一个字段。我浏览了论坛并看到了从 imprts 中删除 android.R 的建议。现在我收到一条错误消息:

方法 MediaPlayer(myTest1Main, int) 未定义类型 myTest1Main

最佳答案

应该是:

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

关于android - 为媒体播放器导入原始 mp3 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5429131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com