简体   繁体   English

构建工作正常,但媒体播放似乎未加载

[英]Build works, but media play doesn't seem to load

i'm working on an android app at the moment that has the purpose of playing a particular song at exactly midnight. 我目前正在开发一个Android应用,目的是在午夜恰好播放一首特定的歌曲。 I know that there might already be apps in the appstore that serve that purpose, but I'm new to coding and wanted to try to program the app myself. 我知道应用商店中可能已经有满足此目的的应用,但是我是编码的新手,想尝试自己编写应用。 So now I reached the point where I don't know what to do anymore. 所以现在我到达了一个不知道该做什么的地步。 I tried debugging the App on my LG H818P running Android 6.0. 我尝试在运行Android 6.0的LG H818P上调试该应用程序。 It posted, The Icon, the Name and everything were correct, but i didn't play the song, even when i set the time when it should've played it to two minutes after the start if the building. 它发布了“图标”,“名称”和所有内容都正确,但是我没有播放这首歌,即使我将它应该播放的时间设置为在建筑物开始后两分钟也是如此。

So that's the code of the MainActivity.cs in Visual Studio 2017, can anyone find any errors? 这就是Visual Studio 2017中MainActivity.cs的代码,有人可以找到任何错误吗?

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Media;

namespace Name of the App
{
    [Activity(Label = "Name of the App", MainLauncher = true)]
    public class MainActivity : Activity


    {
        string text = "status";
        protected void onCreate(Bundle savedInstanceState)

        {


        }

        public void main()
        {
            try
            {

                string systemtime = DateTime.Now.ToString();

                for (int i = 0; i > 0; i++)
                {
                    if (systemtime == "09:07:00 pm")
                    {
                        StartPlayer();
                        player.Start();
                    }
                }
            }
            catch
            {
                text = "Error!";
            }
        }

        protected MediaPlayer player;
        public void StartPlayer()
        {
            if (player == null)
            {
                player = new MediaPlayer();
                player.SetDataSource("Ressources.raw.file2beplayed.mp3");
                player.Prepare();
                player.Start();
                text = "Playing!";
            }
            else
            {
                player.Reset();
                player.SetDataSource("Ressources.raw.file2beplayed.mp3");
                player.Prepare();
                player.Start();
            }
        }

    }


}

As I said, I'm a Noob in coding, so sorry for the maybe ugly code :) Thanks for your replies! 就像我说的那样,我在编码方面是个菜鸟,所以为可能丑陋的代码感到抱歉:)感谢您的答复!

The default format for ToString in a DateTime is MM/dd/yyyy , so your comparation systemtime == "09:07:00 pm" will never be true. DateTime ToString的默认格式为MM/dd/yyyy ,因此您的比较systemtime == "09:07:00 pm"将永远都不是。

You can use a TimeSpan and the property TimeOfDay from DateTime ; 您可以使用TimeSpanDateTime的属性TimeOfDay

if(DateTime.Now.TimeOfDay == new Timespan(21,7,0))
  //...

Also, your loop will never be executed as you initialize i as 0, so at the first check i will not be greater than 0 so the loop isn't executed. 另外,在将i初始化为0时,将永远不会执行循环,因此在第一次检查时i不会大于0,因此不会执行循环。

Finally, Android does not use the main function, is an special type of program, your initialization code should be in the OnCreate function, but if you create an infinite loop in that function Android will close the application as it will not finish it's initialization, you need to use a timer and check the condition each second. 最后,Android不使用main函数,它是一种特殊的程序类型,您的初始化代码应该在OnCreate函数中,但是如果您在该函数中创建无限循环,Android将关闭应用程序,因为它不会完成其初始化,您需要使用计时器并每秒检查一次状况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM