首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >音乐文件播放45秒,然后在android应用程序中停止

音乐文件播放45秒,然后在android应用程序中停止
EN

Stack Overflow用户
提问于 2021-08-16 03:38:17
回答 2查看 49关注 0票数 0

我正在尝试用android studio和Java在我的android手机上做一个游戏。当我把一个音乐文件放到应用程序中时,它会播放,但它只播放了45秒,然后就停止了。我已经在其他几个音乐文件上测试了它,看看它是否以某种方式是音乐文件,但我仍然得到相同的错误。我的目标是让音乐像在游戏中一样循环播放。我已经尝试了各种youtube视频,并查看了其他网站,但我仍然得到相同的问题或无法编译代码。

代码语言:javascript
运行
复制
package com.example.androidstudio2dgamedevelopment;


import static android.media.MediaPlayer.*;

import android.animation.Animator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.media.MediaPlayer;

import android.view.WindowManager;

import java.util.Vector;

/**
 * Main Activity is the entry point to our application
 */
public class MainActivity extends Activity {

    private int width;
    private int height;

    MediaPlayer bkgrdmsc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MediaPlayer mediaPlayer = create(this, R.raw.fg);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();

        //Set Window to fullscreen (will hide status bar)
        Window window = getWindow();
                this.width = (240);
                this.height = (240);

        //Set Content view to game, so that objects in the Game Class can be rendered to the screen
        setContentView(new Game(this));
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-18 04:18:33

我知道是什么了。下面是我使用的代码,它对我很有效。我往往无法找到我的问题的答案,直到我问他们。

代码语言:javascript
运行
复制
package com.example.androidstudio2dgamedevelopment;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Window;

/**
 * Main Activity is the entry point to our application
 */
public class MainActivity extends Activity {

    private int width;
    private int height;

    MediaPlayer mysong;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mysong = MediaPlayer.create(MainActivity.this, R.raw.bc);
        mysong.start();
        mysong.setLooping(true);

        //Set Window to fullscreen (will hide status bar)
        Window window = getWindow();
                this.width = (240);
                this.height = (240);

        //Set Content view to game, so that objects in the Game Class can be rendered to the screen
        setContentView(new Game(this));
    }

    @Override
    protected void onPause() {
        super.onPause();
        mysong.release();
        finish();
    }
}

这是我认为我做错了的地方。

我使用MediaPlayer而不是mysong作为变量,这似乎对我很有效。在我发布的代码中,我还忘记在this之前添加MainActivity

代码语言:javascript
运行
复制
        mysong = MediaPlayer.create(MainActivity.this, R.raw.bc);
        mysong.start();
        mysong.setLooping(true);

我还在最后添加了这个。

代码语言:javascript
运行
复制
    @Override
    protected void onPause() {
        super.onPause();
        mysong.release();
        finish();
    }
票数 0
EN

Stack Overflow用户

发布于 2021-08-16 03:52:52

只需为媒体播放器添加setLooping(true)即可。

代码语言:javascript
运行
复制
Uri uri = createUri(context, R.raw.fg);
Mediaplayer mediaPlayer= new MediaPlayer();
mediaPlayer.setDataSource(context, uri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
mediaPlayer.start();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68797146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档